/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import config.Models.Conf_Cote;
import config.Models.Conf_Fish;
import config.Models.Conf_Ibshop_item;
import db.Redis_Rd;
import db.Redis_W;
import entities.Cote;
import entities.Penguin;
import entities.User;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import libCore.Util;
import share.KeysDefinition;
import share.ShareConstants;
import share.ShareMacros;
import utilities.time.UtilTime;

/**
 *
 * @author LinhTA
 */
public class U_IbshopModel implements BaseModel {

    public String uId;

    public U_IbshopModel(String uid) 
    {
        uId = uid;
    }

    @Override
    public void setDB() 
    {
    }

    @Override
    public void setData(Map<String, String> obj) 
    {
    }

    @Override
    public void getDB(String uid) 
    {
    }
    
    //===================== Hieu =============================
    /*
     * Return penguin id if success
     */
    public String buyPenguinByGold( String itemId, String coteId ) 
    {
        String pengId   =   ShareConstants.EMPTYDATA;
        
        if( !UserModel.isExist( uId ) )
            return pengId;
        
        UserModel uModel    =   new UserModel( uId );
        User user   =   uModel.getDBUser();
        if( user == null )
            return pengId;
        
        int uGold   =   user.gold;
        HashMap<String, Conf_Ibshop_item> itemsConf =   config.ConfigModels.conf_ibshopItems;
        Conf_Ibshop_item confItem   =   itemsConf.get( itemId );
        if( confItem.kind.equals( "penguin" ) )
        {
            String pengType =   confItem.entity;
            int confGold    =   confItem.require.gold;

            Cote.CoteDB  coteDB     =   new Cote.CoteDB();
            U_CoteModel coteModel   =   new U_CoteModel( uId );
            Cote cote   =   coteModel.getCote( coteId, coteDB );
            if( cote == null )
                return pengId;

            // Check for number peng in cote
            HashMap<String, Conf_Cote> confCote =   config.ConfigModels.conf_cotes;
            int currentCoteConfPeg =   confCote.get( String.valueOf(cote.level) ).maxPeg;

            long time   =   UtilTime.getTimeNow();
            
            Penguin peng=   U_PenguinModel.createPeg( pengType, 0, time );
            if( currentCoteConfPeg <= cote.pegs.size() || uGold < confGold || peng == null )
                return pengId;
            // Insert to cote for update
            cote.pegs.put( pengId, peng );

            // Buy penguin and add to redis
            String pengRedis    =   Util.obj2String(peng);
            pengId       =   KeysDefinition.randomKeySTR();
            Redis_W.getInstance().set( pengId, pengRedis );

            // Add penguin to redis pengsId of coteId
            Redis_W.getInstance().sadd( coteDB.pegs, pengId );

            // pay gold when success
            uGold      -=   confGold;
            user.gold   =   uGold;
            uModel.updateDBUser( user );
            //UserModel.updateUserGold( uid, uGold );

            // Increase user exp
            // U_CoteModel.updateCoteAndDB(uid, coteId, cote, coteDB);
        }
        return pengId;
    }
    
    /*
     * Return peng Id if success
     */
    public String buyPenguinByCoin( String itemId, String coteId ) 
    {
        String pengId   =   ShareConstants.EMPTYDATA;
        String keyUser  =   KeysDefinition.getKeyUser( uId );
        Set<String> strs = Redis_Rd.getInstance().smember( ShareMacros.USERS );
        if( strs.contains( keyUser) )
        {
            UserModel uModel    =   new UserModel( uId );
            int uCoin   =   uModel.getDBUser().coin;
            HashMap<String, Conf_Ibshop_item> itemsConf =   config.ConfigModels.conf_ibshopItems;
            Conf_Ibshop_item confItem   =   itemsConf.get( itemId );
            if( confItem.kind.equals( "penguin" ) )
            {
                String pengType =   confItem.entity;
                int confCoin    =   confItem.require.coin;
                
                Cote.CoteDB  coteDB     =   new Cote.CoteDB();
                U_CoteModel coteModel   =   new U_CoteModel( uId );
                Cote cote   =   coteModel.getCote( coteId, coteDB );
                if( cote == null )
                    return pengId;
                
                // Check for number peng in cote
                HashMap<String, Conf_Cote> confCote =   config.ConfigModels.conf_cotes;
                int currentCoteConfPeg =   confCote.get( String.valueOf(cote.level) ).maxPeg;
                
                long time       =   UtilTime.getTimeNow();
                
                Penguin peng=   U_PenguinModel.createPeg( pengType, 0, time );
                if( currentCoteConfPeg <= cote.pegs.size() || uCoin < confCoin || peng == null )
                    return pengId;
                
                // Buy penguin and add to redis
                String pengRedis    =   Util.obj2String(peng);
                pengId       =   KeysDefinition.randomKeySTR();
                Redis_W.getInstance().set( pengId, pengRedis );
                
                // Add penguin to redis pengsId of coteId
                Redis_W.getInstance().sadd( coteDB.pegs, pengId );
                
                // pay gold when success
                uCoin      -=   confCoin;
                uModel.updateUserCoin( uCoin );
            }
        }
        return pengId;
    }
    
    
    public Map<String, Object> buyFish( String itemType ) 
    {
        
        Map<String, Object> result  =   new HashMap<String, Object>();
        
        UserModel uModel    =   new UserModel( uId );
        User um             =   uModel.getDBUser();
        
        Conf_Ibshop_item confItem  =   config.ConfigModels.conf_ibshopItems.get( itemType );
        
        Boolean canBuy      =   false;
        
        if( um.gold >= confItem.require.gold )
        {
            um.gold         -=  confItem.require.gold;
            result.put( ShareConstants.PAYMENT_TYPE_GOLD, confItem.require.gold );
            
            Conf_Fish conf_fish =   config.ConfigModels.conf_fishs.get( confItem.entity );
            um.fish         +=  conf_fish.food;
            result.put( ShareConstants.FISH, conf_fish.food );
            canBuy          =   true;
            // Update user info
            uModel.updateDBUser( um );
        }
        result.put( ShareConstants.CAN_BUY, canBuy );
        System.out.println( "---------> Result buy fish " + (canBuy?"success":"failed") + ": " + itemType );
        
        return result;
    }
    
    //===================== Hieu =============================
}
